import { getDefaultFields } from '../utils';

// 用户-优惠券关联表设计
module.exports = app => {
  const DataTypes = app.Sequelize;

  const Model = app.model.define('user_coupon', {
    id: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      primaryKey: true,
      autoIncrement: true,
    },
    user_id: {
      type: DataTypes.STRING(50),
      allowNull: false,
    },
    coupon_id: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
    },
    ...getDefaultFields()
  }, {
    tableName: 'user_coupon',
    timestamps: false,
  });

  Model.sync({ alter: true });
  Model.associate = function() {
    app.model.UserCoupon.hasOne(app.model.SmsCoupon, {
      foreignKey: 'id',
      constraints: false,
      sourceKey: 'coupon_id'
    });
  };

  return Model;
};
